#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../include/vfs.h"
 
 
Trie getNode()
{
    Trie new;
 
    new = malloc(sizeof(struct trie_node));
 
    if( new )
    {
        int i;
 
        new->value = 0;
 
        for(i = 0; i < 26; i++)
        {
            new->children[i] = NULL;
        }
    }
 
    return new;
}

void initialize(trie_structure *initial)
{
    initial->root = getNode();
    initial->count = 0;
}
 

void insert_trie(trie_structure *initial, char word[])
{
    int level;
    int length = strlen(word);
    int index;
    char ch;
    Trie temp;
 
    initial->count++;
    temp = initial->root;

    //printf("\n IN INSERTION:");	
 
    for( level = 0; level < length; level++ )
    {	
        ch = word[level];
  
        index = ((int)ch - (int)'a');

	if(index>=0 && index<=26)
	{
	  // printf("\n gettin inserted %c",word[level]);

        	if( !temp->children[index] )
        	{
            	temp->children[index] = getNode();
        	}
 
	        temp = temp->children[index];
	}

    }
 
    
    temp->value = initial->count;
}
 

int trie_search(trie_structure *initial, char isword[])
{
    int level;
    int length = strlen(isword);
    int index;
    char ch;
    Trie temp;
 
    temp = initial->root;
 
    for( level = 0; level < length; level++ )
    {
        ch = isword[level];
  
        index = ((int)ch - (int)'a');
        
        if(index>=0 && index<=26)
     {
 
        if( !temp->children[index] )
        {
            return 0;
        }
 
        temp = temp->children[index];
    }
   }
 
    return (0 != temp && temp->value);
}

